<?php

class PageView{
	
	#Gerenate Page form for admin pages
	function genertatePageForm(){
		
		global $PROJECT_DB_TABLES;
		
		$output = "";
		
		$output .= "<div id=\"DIV_ADMINPAGES_CONTENT\" align=\"left\">";
		
		$output .= "<div id=\"DIV_ADMINPAGES_LEFT\">";
		
		$output .= "<table cellspacing=\"0\" align=\"left\">";
		
		$current_data = PageData::getSections(true);
		
		if(!empty($current_data)){
		
			foreach($current_data as $current_key=>$current_value){
				
				$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><div class=\"admin_pages_closed_dir\" onclick=\"onlick_expandParentPage('".$current_value."',this,'')\"></div></td><td style=\"border-color: white;border-style: solid;\">".$current_value."<br/><div></div></td></tr>";
			}
		}
		
		$output .= "</table>";
		
		$output .= "</div>";
		
		$output .= "<div id=\"DIV_ADMINPAGES_RIGHT\">";
		
		$output .= "</div>";
		
		$output .= "</div>";
		
		return $output;
	}
	
	/**
	*generatePageMoveForm() generate move section form for pages by Adrian
	*@param $pageID selected page id
	*
	*/
	function generatePageMoveForm($pageID = ""){
		
		$obPage = PageData::getPageDetailsWithPath($pageID);
		
		$pageSection = $obPage -> getData("p_section");
		
		$pageName = $obPage -> getData("p_name");
		
		$pageLayer = $obPage -> getData("p_layer");
		
		$output = "";
		
		$output .= "<div class=\"MoveDivFrame\">";
		
		$output .= "<table cellspacing=\"0\" align=\"left\">";
		
		$arrSection = PageData::getSections(false);
		
		foreach($arrSection as $value){
			
			if($value == $pageSection){
				
				$output .= "<tr><td valign=\"top\" class=\"movetd\"><div class = \"admin_pages_open_dir\" onclick=\"onlick_openPageDir(this,'".$value."','".$pageID."',true);\"></div></td><td class=\"movetd1\" align=\"left\">".$value."";
				
				$output .= $this -> iterateGenerateSubLeaves($obPage,0);
				
				$output .= "<div></div></td></tr>";
			}
			else{
				
				$output .="<tr><td valign=\"top\" class=\"movetd\"><div class = \"admin_pages_closed_dir\" onclick=\"onlick_openPageDir(this,'".$value."','".$pageID."',true);\"></div></td><td class=\"movetd1\" align=\"left\">".$value."<div>";
				
				$output .= "</div></td></tr>";
			}
		}
		
		$output .= "</table>";
		
		$output .= "</div>";
		
		return $output;
	}
	
	/**
	*Use iteration method to generate sub leaves of pages tree
	*@param $obPage is the object of page
	*
	*
	*/
	function iterateGenerateSubLeaves($obPage,$counter){
		
		$arrPagePath = $obPage -> getData("p_path");
		
		//print_r($arrPagePath);
		
		$arrCurQueue = $arrPagePath;
		
		$currentKey = key($arrCurQueue);
		
		end($arrPagePath);
		
		$lastKey = key($arrPagePath);
		
		for($i=0; $i < $counter; $i++){
			
			next($arrCurQueue);
		}
		
		$obPageList = PageData::getPageListAtDepth($obPage -> getData("p_section"),$counter);
		
		foreach($obPageList -> getPagesArray() as $value){
			
			$thisPath = current($arrCurQueue);
			
			print_r($arrCurQueue);
			
			if($value -> getID() == $thisPath["p_path"]){
				
				$output .= "<tr><td valign=\"top\" class=\"movetd\"><div class = \"admin_pages_open_dir\" onclick=\"onlick_openPageDir(this,'".$value -> getData("p_section")."','".$value -> getID()."',true);\"></div></td><td class=\"movetd1\" align=\"left\">".$value -> getData("p_name")."";
				
				//$output .= $this -> iterateGenerateSubLeaves($obPage,0);
				
				$output .= "<div></div></td></tr>";
			}
			else{
				
				$output .="<tr><td valign=\"top\" class=\"movetd\"><div class = \"admin_pages_closed_dir\" onclick=\"onlick_openPageDir(this,'".$value -> getData("p_section")."','".$value -> getID()."',true);\"></div></td><td class=\"movetd1\" align=\"left\">".$value -> getData("p_name")."<div>";
				
				$output .= "</div></td></tr>";
			}
		}
		
		
		
		if($currentKey == $lastKey){
			
			echo $currentKey."----".$lastKey; 
		}
		else{
		
			foreach($arrCurQueue as $value){
				
				//print_r($value);
				
				//echo "<br/>";
				
				
				
				//if(key($arrCurQueue) == $lastKey){
//					
//					echo $currentKey."----".$lastKey."<br/>";
//					
//					$counter ++;
//			
//					$this -> iterateGenerateSubLeaves($obPage,$counter);
//				}
//				else{
//					
//					echo key($arrCurQueue)."==".$lastKey."<br/>";
//				
//					next($arrCurQueue);
//					
//					echo key($arrCurQueue)."==".$lastKey."<br/>";
//				}

			}
			
			
			//$output = "<table cellspacing=\"0\" align=\"left\">";
			
			//$arrSection = PageData::getSectionPages($obPage -> getData("p_section"),$curPage["path"]);
			
			//$output .= "</table>";
		}
	}
	
	#Gerenate Page move form 
	function genertatePageMoveForm($parent = "", $section = ""){
		
		global $SubDirContent;
		
		global $pagePath;
		
		global $pagesMove;
		
		global $count;
		
		$pagesChildren = $pagesMove -> getChildrenOfPage($parent);
		
		$SubDirContent.= "<table cellspacing=\"0\" align=\"left\">";
		
		foreach ($pagesChildren -> getPageIDs() as $pageID){
			
			$obPage = $pagesChildren -> getPage($pageID);#Get page object
			
			$obPageSection = $obPage -> getData("p_section");#Get page section
			
			$obPageId = $obPage -> getData("p_id");#Get page id
			
			$obPageName = $obPage -> getData("p_name");#Get page name 
			
			if($obPageSection == $section){
				
			  $obPageChildrenCon =  count($pagesMove -> getChildrenOfPage($obPageId) -> getPageIDs());#The mount of children 
			
			  if($pagePath[$count] == $obPageId){ 
				  
				  if($count != count($pagePath)-1){
				  
						if($obPageChildrenCon == 0){
				  
							$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/document.png\");\"/></td><td class=\"movetd1\" align=\"left\">";
						}
						else{
					 
							$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/folder_open.png\");\" onclick=\"onlick_openPageDir('".$obPageId."',false,true);\"/></td><td class=\"movetd1\" align=\"left\">"; 
						}
				  
						$SubDirContent.="<a id=\"a_".$obPageId."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$obPageId."','".$parent."','".$section."');\">".$obPageName."(".$obPageId.")"."</a><br/><div align=\"left\" id=\""."div".$obPageId."\">";
				  
						$count ++;
						
						genertatePageMoveForm($obPageId,$section);
				  }
				  else{
					  
						if($obPageChildrenCon == 0){
				  
							$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/document.png\");\"/></td><td class=\"movetd1\" align=\"left\">";
						}
						else{
					 
							$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/folder_closed.png\" onclick=\"onlick_openPageDir('".$obPageId."',false,false);\");\"/></td><td class=\"movetd1\" align=\"left\">"; 
						}
				  
						$SubDirContent.="<a id=\"a_".$obPageId."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$obPageId."','".$parent."','".$section."');\">".$obPageName."(".$obPageId.")"."</a><br/><div align=\"left\" id=\""."div".$obPageId."\">";
				  }
				  
				  $SubDirContent .= "</div></td></tr>";
			  }
			  else{
				  
				  if($obPageChildrenCon == 0){
				  
						$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/document.png\");\"/></td><td class=\"movetd1\" align=\"left\">";
				  }
				  else{
					 
						$SubDirContent.="<tr><td valign=\"top\" class=\"movetd\"><img id=\""."img_".$obPageId."\" src=\"../assets/images/tree/folder_closed.png\");\" onclick=\"onlick_openPageDir('".$obPageId."',false,false);\"/></td><td class=\"movetd1\" align=\"left\">"; 
				  }
				  
				  $SubDirContent.="<a id=\"a_".$obPageId."\" onclick=\"\" style=\"text-decoration:none;\" href=\"javascript:itemSelected('".$obPageId."','".$parent."','".$section."');\">".$obPageName."(".$obPageId.")"."</a><br/><div align=\"left\" id=\""."div".$obPageId."\">";
				  
				  $SubDirContent .= "</div></td></tr>";
			  }
			}
		}
		
		$SubDirContent.= "</table>";
	}
	
	/**
	 * generatePageTree() function is used only for the page edit model and page create model for user to select parent of page
	 *
	 * @return the output
	*/
	function generatePageTree($current_parent="",$current_section="") {
		
		# Globals
		//global $PASSED_ID;
		
		global $PROJECT_DB_TABLES;
	
		# Output
		$output = "<tr class=\"norightborder\"><td>Select Parent:</td>";
	
		//# Flash URL
//		$flash_url = $flash_filename = "../assets/flash/page_move_01.swf?mode=select&host=../&passed_width=300&passed_height=300&selected_page_id=parentof:" . $PASSED_ID;
	
		# Add flash page selector
		$output .= "<td align=\"left\">";//. generatePageMoveFlash($flash_url, 300, 300);
		
		# Add Hidden Fields
		$output .= "<input type=\"text\" name=\"p-parent\" id=\"p-parent\" value=\"".$current_parent."\" />";
		
		$output .= "<input type=\"text\" name=\"p-section\" id=\"p-section\" value=\"".$current_section."\" />";
		
		$output .= "<input type=\"text\" name=\"p-layer\" id=\"p-layer\" value=\"0\" />";
		
		#Test HTML PAGE
		$output .= "<div align=\"left\" id=\"dvPagesTree\"  class = \"admin_pages_select_div\">";
		
		$output .= "<table cellspacing=\"0\" align=\"left\">";
		
		$current_data = PageData::getSections(false);
		
		foreach($current_data as $current_key=>$current_value){
			
			$output.="<tr><td valign=\"top\" style=\"border-color: white;border-style: solid;\"><div class=\"admin_pages_closed_dir\" onclick=\"onlick_openPageDir(this,'".$current_value."','',false);\"></div></td>";
			
			$output.="<td style=\"border-color: white;border-style: solid;\"><a onclick=\"onclick_selectPage(this,'','".$current_value."',0)\" href=\"javascript:void(0);\" style=\"text-decoration:none;\">".$current_value."</a><br/><div></div></td></tr>";
		}//".$current_value["p_id"]."
		
		$output .= "</table>";
		
		$output .= "</div>";
		
		$output .= "<div>";
		
		$output .= "SELECTED ITEM:<label id=\"lblSelected\"></label>";
		
		$output .= "</div>";
		
		# Close
		$output .= "</td></tr>";
	
		return $output;
	
	}
}

?>